﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Reflection;

namespace HSDc.RingleFlow.Kernel.Persistence
{
    internal class PersistHelper
    {
        internal static WFPersitable GetPersist(string configName)
        {
            return (WFPersitable)GetObject(configName);
        }
        private static Object GetObject(string configName)
        {
            PersistHandler configSection = new PersistHandler();
            string configPath = configSection.GetType().Assembly.CodeBase.Substring(8).Replace("/", "\\");
            Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(configPath);
            string className = (string)config.GetSection(configName).ElementInformation.Properties["className"].Value;
            AssemblyName myAssemblyName = new AssemblyName();
            myAssemblyName.Name = config.GetSection(configName).ElementInformation.Properties["dllName"].Value.ToString();
            myAssemblyName.CodeBase = configSection.GetType().Assembly.CodeBase;
            //myAssemblyName.CodeBase = myAssemblyName.CodeBase.Substring(0, myAssemblyName.CodeBase.LastIndexOf("/")) +"/"+ myAssemblyName.Name + ".DLL";
            Assembly instance = Assembly.Load(myAssemblyName);
            return instance.CreateInstance(className);
        }
    }
}
